package com.yitiao.ratelimit;

import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class RateLimiterTest {

    @Test
    @SneakyThrows
    public void FunnelTryAcquire() {
        FunnelRateLimiter limiter = new FunnelRateLimiter(100, 0.1);
        assertTrue(limiter.tryAcquire(10));
        assertFalse(limiter.tryAcquire(100));
        assertTrue(limiter.tryAcquire(10));

        Thread.sleep(2000);
        assertTrue(limiter.tryAcquire(10));
        System.out.println(limiter.getLeftQuota());
    }

    @Test
    public void bucketTryAcquire() {
        TokenBucket bucket = new TokenBucket(10, 2);
        assertTrue(bucket.acquire(5));
        assertTrue(bucket.acquire(3));
        assertFalse(bucket.acquire(5));
        assertTrue(bucket.acquire(2));
        assertFalse(bucket.acquire(1));
    }
}


